/*
 * Copyright (c) 2017.
 * @author Hulunliang
 * @since 2017/11/4
 *
 */
$(function(){
    var parentHeight,parentWidth;
    parentHeight = $("#cooperatorStaffer-box").height() - 73;
    parentWidth = $("#cooperatorStaffer-box").width() + 14;
    $("#cooperatorStaffer-table").jqGrid({
        url: contextPath + "/CooperatorStaffer/queryCooperatorStafferJqg",
        datatype: "json",
        height: parentHeight,
        width: parentWidth,
        colNames:['','','单位名称','姓名','项目名称','手机号','邮箱','从事工作','服务时长','备注','操作'],
        colModel:[
            {name:"id",hidden:true},
            {name:'iconFile',hidden:true},
            {name:'companyName',index:'companyName',align:"center"},
            {name:'name',index:'name',align:"center"},
            {name:'projectName',index:'projectName',align:"center"},
            {name:'mobile',index:'mobile',align:"center"},
            {name:'email',index:'email',align:"center"},
            {name:'workAs',index:'workAs',align:"center"},
            {name:'duration',index:'duration',align:"center"},
            {name:'note',index:'note',align:"center"},
            {name:'operate',index:'operate',align:"center",formatter: operation}

        ],
        sortname:'id',
        sortorder:'asc',
        viewrecords: true,
        rowNum: 20,
        rowList:[20,50,100],
        pager: '#cooperatorStaffer-pager'
    });
    $("#cooperatorStaffer-table").jqGrid('navGrid', '#cooperatorStaffer-pager', {edit : false,add : false,del : false,search: false});

    // 查询按钮
    $("#query").on("click",function(){
        $("#cooperatorStaffer-table").setGridParam({
            url : contextPath + "/CooperatorStaffer/queryCooperatorStafferJqg",
            postData : {
                queryCond:{
                    'companyName': $('#companyName').val(),
                    'name': $('#name').val()
                }
            },
            page: 1
        }).trigger("reloadGrid");
    });
});

function operation(cellvalue,options,rowObject){
    var str = "";
    // if([[${#authorization.expression('hasAuthority(''modifyCooperatorStaffer'')')}]]){
    str += "<input class='btn btn-primary btn-xs modify' type='button' value='修改'>";
    // }
    // if([[${#authorization.expression('hasAuthority(''deleteCooperatorStaffer'')')}]]){
    str += "<input class='btn btn-danger btn-xs del' type='button' value='删除' data-toggle='modal' data-target='#deleteNotice'>";
    // }
    return str;
}
//修改
var selectedData = null;
/* 修改 || 删除 */
$("#cooperatorStaffer-table").on('click','.modify,.del',function(){
    var id = $(this).parent().parent().attr('id');
    selectedData = $("#cooperatorStaffer-table").jqGrid('getRowData',id);
    delete selectedData.operate;
    if($(this).hasClass("modify")){
        window.location.hash = "qualitycontrol-cooperatorGroup-modifyCooperatorStaffer";
    }else{
//    	selectedData.id = Number(selectedData.id);
    }
    console.log(selectedData);
});
$("#deleteData").on('click',function(){
    $.ajax({
        url:contextPath + '/CooperatorStaffer/deleteCooperatorStaffer',
        type:"delete",
        contentType:"application/json",
        dataType:"json",
        data: JSON.stringify(selectedData),
        success:function(data){
            if(data.code == 1){
                $('#deleteNotice').modal('hide');
                toastr.success('删除成功！');
                // $("#cooperatorStaffer-table").trigger("reloadGrid");
                $("#cooperatorStaffer-table").setGridParam({
                    url : contextPath + "/CooperatorStaffer/queryCooperatorStafferJqg",
                    postData : {
                        queryCond:{
                            'companyName': $('#companyName').val(),
                            'name': $('#name').val()
                        }
                    },
                    page: 1
                }).trigger("reloadGrid");
                selectedData = null;
            }else{
                toastr.error(data.message,'删除失败！');
            }
        },
        error:function(data){
            console.log(data);
            toastr.error('操作失败，请稍后再试！');
        }
    });
});

//函数：获取尺寸　
var timer = 0;
$(window).resize(function(){
    clearTimeout(timer);
    timer = setTimeout(function() {
        $("#cooperatorStaffer-table").setGridWidth($('#cooperatorStaffer-box').width());
        $("#cooperatorStaffer-table").setGridHeight($('#cooperatorStaffer-box').height() - 73);
    }, 260);
});
// 侧边栏缩小时，实时的给jqgrid的宽和高赋值
$("#asidebarSwitch").on('click',function() {
    var time = 0;
    var set = setInterval(function (){
        $("#cooperatorStaffer-table").setGridWidth($('#cooperatorStaffer-box').width());
        $("#cooperatorStaffer-table").setGridHeight($('#cooperatorStaffer-box').height() - 73);
        time++;
        if(time == 201){
            clearInterval(set);
        }
    },time);
});

